package bug
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	
	public class Scene extends Sprite
	{
		private var red:Vector.<Ant> = new Vector.<Ant>();
		private var blue:Vector.<Ant> = new Vector.<Ant>();
		internal var _castle:Vector.<Castle> = new Vector.<Castle>();
		private var _bg:DisplayObject;
		private var _path:DisplayObject;
		
		private var _power:int;
		public function Scene()
		{
		}

		public function setBg(bg:DisplayObject):void{
			if(_bg && _bg.parent){
				_bg.parent.removeChild(bg);
			}
			_bg=bg;
			addChildAt(bg,0);
		}
		
		public function setPath(path:DisplayObject):void{
		    if(_path && _path.parent){
			   _path.parent.removeChild(path);
			}
			_path=path;
			addChild(path);
		}
		
		public function dispose():void{
			red = new Vector.<Ant>();
			blue = new Vector.<Ant>();
			for (var i:int = 0; i < _castle.length; i++) 
			{
				_castle[i].dispose();
			}
			_castle = new Vector.<Castle>();
		}
		
		
		public function step():Boolean{
			var temp:Ant;
			for (var i:int = 0; i < red.length; i++) 
			{
				temp = red[i];
				temp.updata();
				for (var j:int = 0; j < blue.length; j++) 
				{
					if(temp.hitTestObject(blue[j])){
						temp.setAttackTarget(blue[j]);
						blue[j].setAttackTarget(temp);
					}
				}
				
				if(temp.isDead || temp.checkCastleHit()){
					red.splice(i,1);
					i--;
					temp.release();	
				}
			}
			
			for(i = 0; i < blue.length; i++) 
			{
				temp=blue[i]
				temp.updata();
				if(temp.checkCastleHit() || temp.isDead){
					blue.splice(i,1);//挂了
					i--;
					temp.release();
				}
			}
			return checkOver();
		}
		
		private function checkOver():Boolean{
			
			for (var i:int = 0; i < _castle.length; i++) 
			{
				if(_castle[i]._power==_power){
				    //isWin;
					return false;
				}
			}
			return true
		}
		public function addAnt(ant:Ant):void{
			if(ant.power==PowerEnum.ME){
				red.push(ant);
			}else{
			    blue.push(ant);		
			}
			addChild(ant);
		}
	
		public function addCastles(castles:Vector.<Castle>):void{
			_castle=castles;
		}
	
		
		public function moveAnt(_castle:Array):void
		{
			var amount:int=0;
			for (var i:int = 0; i < _castle.length; i++) 
			{
				_castle[i][0].createAntTo(_castle[i][1]);
			}
		}
	}
}